package com.love.cloud.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.cloud.admin.api.entity.SysRoleZzdUser;
import com.love.cloud.admin.api.entity.SysUser;
import com.love.cloud.admin.mapper.SysRoleZzdUserMapper;
import com.love.cloud.admin.service.DingTalkService;
import com.love.cloud.admin.service.SysRoleZzdUserService;
import com.love.cloud.admin.service.SysUserService;
import com.love.cloud.common.core.constant.CommonConstants;
import com.love.cloud.common.core.exception.BusinessException;
import com.love.cloud.common.mybatis.util.PageUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统角色和浙政钉关联表(SysRoleZzdUser)表服务实现类
 *
 * @author hongdongqu
 * @since 2022-04-12 15:50:29
 */
@Service
public class SysRoleZzdUserServiceImpl extends ServiceImpl<SysRoleZzdUserMapper, SysRoleZzdUser> implements SysRoleZzdUserService {

    @Resource
    private PageUtils pageUtils;

    @Resource
    private SysUserService sysUserService;
    @Resource
    @Lazy
    private DingTalkService dingTalkService;

    /**
     * 分页查询所有数据
     *
     * @param sysRoleZzdUser 查询实体
     * @return 所有数据
     */
    public Page<SysRoleZzdUser> pageInfo(SysRoleZzdUser sysRoleZzdUser){
        Page page = pageUtils.page();
        QueryWrapper<SysRoleZzdUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(sysRoleZzdUser.getRoleId()!=null, SysRoleZzdUser::getRoleId,sysRoleZzdUser.getRoleId());
        return this.page(page,queryWrapper);
    }

    /**
     * 新增数据
     *
     * @param sysRoleZzdUser 实体对象
     * @return 新增结果
     */
    public Object add(SysRoleZzdUser sysRoleZzdUser){
        boolean save = this.save(sysRoleZzdUser);
        List<SysUser> sysUsers = this.handleUpdateSysUserStatus(sysRoleZzdUser.getZDingAccountId(), CommonConstants.STATUS_NORMAL);
        //如果系统没有当前浙政钉id的用户就直接添加用户
        if(sysUsers==null||sysUsers.size()==0){
            dingTalkService.addUserByAccountId(sysRoleZzdUser.getZDingAccountId(),null);
        }
        return save;
    }

    @Override
    public Object addBatch(List<SysRoleZzdUser> sysRoleZzdUserList) {
        for (SysRoleZzdUser sysRoleZzdUser:sysRoleZzdUserList) {
            this.add(sysRoleZzdUser);
        }
        return "新增成功";
    }

    /**
     * 修改数据
     *
     * @param sysRoleZzdUser 实体对象
     * @return 修改结果
     */
    public Object update(SysRoleZzdUser sysRoleZzdUser){
        return this.updateById(sysRoleZzdUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object delete(Long id) {
        //获取到数据
        SysRoleZzdUser sysRoleZzdUser = this.getById(id);
        if(sysRoleZzdUser==null){
            throw new BusinessException("请正确的id");
        }
        //执行删除逻辑
        this.removeById(id);
        this.handleUpdateSysUserStatus(sysRoleZzdUser.getZDingAccountId(), CommonConstants.STATUS_LOCK);
        return "删除成功";
    }

    /**
     * 执行更新系统用户状态
     * @param zDingAccountId
     */
    private List<SysUser> handleUpdateSysUserStatus(String zDingAccountId, String status){
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getZDingAccountId,zDingAccountId);
        List<SysUser> list = this.sysUserService.list(queryWrapper);
        for (SysUser sysUser:list) {
            sysUser.setStatus(status);
            this.sysUserService.updateById(sysUser);
            //锁定时候执行清除逻辑
            if(CommonConstants.STATUS_LOCK.equals(status)){
                this.sysUserService.clearUserToken(sysUser);
            }
        }
        return list;
    }
}

